package com.workpaper.app.tray;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.tray.TrayViewConsts;
import com.workpaper.app.utils.platform.SWTPlatformUtils;
/**
 * 系统托盘右键监听
 * @author zyj
 *
 */
public class TrayItemRightClickListener implements Listener{
	/**
	 * 菜单
	 */
	private Menu menu;
	/**
	 * 关闭菜单项
	 */
	private MenuItem closeMenu;
	/**
	 * 还原菜单项
	 */
	private MenuItem showMenu;
	
	
	private SelectionListener  closeMenuListener;
	private SelectionListener  showMenuListener;
	
	
	public TrayItemRightClickListener() {
		super();
		this.menu = new Menu(SWTPlatformUtils.getShell());
		//创建菜单项和监听处理
		closeMenu = new MenuItem(menu, SWT.NONE);
		closeMenu.setText(ViewInfoCache.get(TrayViewConsts.closeMenuText));
		closeMenuListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SWTPlatformUtils.closeApp();
			}
		};
		closeMenu.addSelectionListener(closeMenuListener);
		
		showMenu = new MenuItem(menu, SWT.NONE);
		showMenu.setText(ViewInfoCache.get(TrayViewConsts.showMenuText));
		showMenuListener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				SWTPlatformUtils.showMainWindow();
			}
		};
		showMenu.addSelectionListener(showMenuListener);
	}

	public void dispose() {
		this.closeMenu.removeSelectionListener(closeMenuListener);
		this.showMenu.removeSelectionListener(showMenuListener);
		showMenuListener = closeMenuListener = null;
		this.closeMenu.dispose();
		this.showMenu.dispose();
		closeMenu = showMenu = null;
		this.menu.dispose();
		this.menu = null;
	}
	
	@Override
	public void handleEvent(Event event) {
		menu.setVisible(true);
	}
}
